#!/bin/sh
USERS=
Y=0
fin=./zxc.txt
fxn=`cat ./zxc.txt`
for u in $fxn
do
 	USERS[$Y]=$u
	Y=$(($Y+1))
done

function add_user {
 while true
do
	echo -n "name:"
	read name
	echo -n "gender(male|femal):"
	read gender
	echo -n "age:"
	read age
	
	user_info="$name:$gender:$age"
	USERS[$Y]=$user_info
	Y=$(($Y+1))
	echo "${#USERS[@]}"
	echo -n "(y/n):"
	read answer
 if [ $answer == "n" -o $answer == "N" ]; then
 	break
	fi
done
}
function query_user {
 echo "qing shu ru xing ming"
 read name
	
	for i in ${USERS[@]}
    do
	name1=`echo ${i} | awk -F":" '{print $1}'`
	if [ $name1 == "$name" ];then
	echo ${i}
	break;
	fi
   done
}
function update_user {
 echo -n "qingshurugenxindexueyaun:"
        read name
	for j in ${USERS[@]}
	do
	    name2=`echo ${j} | awk -F":" '{prink $1}'`
	if [ $name2 == "$name" ];then
	     echo -e "(name:gender:age)"
	     read name3
 	     USERS[$j1]="$name3";
	     else
	   j1=$(($j1+1))
	fi
	done
}



while true;
do
	echo "1.tianjia"
	echo "2.chaxun"
	echo "3.genxin"
	echo "4.tuichu"
	echo -n "qing xuanzhe"
	read choice i

 if [ $choice -eq 1 ];then
  	add_user
 elif [ $choice -eq 2 ];then
	query_user
 elif [ $choice -eq 3 ];then
	update_user
 elif [ $choice -eq 4 ];then
echo "baocun(y/n):"
read c
	if [ $c == "y" -o $c == "Y" ];then
	cat /dev/null > $fin
	for ((o=0;$o<$Y	;o++))
	do
		echo ${USERS[$o]} >> $fin
	done
		exit 0
	elif [ $c == "n" -o $c == "N" ];then
	exit
 else
	echo "cuowu"
fi
fi
done
